package com.sdp.irrigation.server.controller;

import java.util.HashSet;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.sdp.core.bizc.vo.Result;
import com.sdp.irrigation.protocol.CommPackage;
import com.sdp.irrigation.server.netty.response.IResponseHandle;
import com.sdp.irrigation.utils.CommConstants;
import com.sdp.irrigation.view.DownVersionParam;

import io.netty.channel.ChannelId;

@RestController
@RequestMapping("/CommandDownlinkController")
public class CommandDownlinkController {
	
	@Autowired
	private IResponseHandle jsonResponseHandle;
	
	/**
	 * 远程升级下发命令
	 * @param downValveParam
	 * @return
	 */
	@PostMapping(value = "/downlinkCommand")
	public Result<?> downlinkCommand(@RequestBody CommPackage msg) {
		if (!StringUtils.isEmpty(msg.getDevSn())) {
			Map<String, Map<String, ChannelId>> no2ChannelMap = jsonResponseHandle.getChannelIdMap();
			if (no2ChannelMap.containsKey(msg.getDevSn()) && no2ChannelMap.get(msg.getDevSn()).size() > 0) {
				jsonResponseHandle.sendMessage(msg.getDevSn(), msg);
				return Result.ok("命令下发成功");
			} else {
				return Result.error("设备不在线！");
			}
		} else {
			return Result.error("devSn为空！");
		}
	}
	
	@PostMapping(value = "/downlinkCommandVersion")
	public Result<?> downlinkCommandVersion(@RequestBody DownVersionParam downVersionParam) {
		if (!StringUtils.isEmpty(downVersionParam.getDevSn())) {
			Map<String, Map<String, ChannelId>> no2ChannelMap = jsonResponseHandle.getChannelIdMap();
			if (no2ChannelMap.containsKey(downVersionParam.getDevSn()) && no2ChannelMap.get(downVersionParam.getDevSn()).size() > 0) {
				downVersionParam.setPakSn(CommConstants.generatePakSn());
				jsonResponseHandle.sendMessage(downVersionParam.getDevSn(), downVersionParam);
				return Result.ok("命令下发成功");
			} else {
				return Result.error("设备不在线！");
			}
		} else {
			return Result.error("devSn为空！");
		}
	}
	
	
	@PostMapping(value = "/downlinkCommandInit")
	public Result<?> downlinkCommandInit(@RequestBody JSONObject obj) {
		String devSn = obj.getString("devSn");
		if (!StringUtils.isEmpty(devSn)) {
			Map<String, Map<String, ChannelId>> no2ChannelMap = jsonResponseHandle.getChannelIdMap();
			if (no2ChannelMap.containsKey(devSn) && no2ChannelMap.get(devSn).size() > 0) {
//				downInitParam.setPakSn(CommConstants.generatePakSn());
				jsonResponseHandle.sendMessage(devSn, obj);
				return Result.ok("命令下发成功");
			} else {
				return Result.error("设备不在线！");
			}
		} else {
			return Result.error("devSn为空！");
		}
	}
	
	@GetMapping(value = "/getChannelIdMap")
	public Result<?> getChannelIdMap() {
		Map<String, Map<String, ChannelId>> no2ChannelMap = jsonResponseHandle.getChannelIdMap();
		Set<String> result = new HashSet<>();
		for (Entry<String, Map<String, ChannelId>> entry : no2ChannelMap.entrySet()) {
			if (entry.getValue().size() > 0) {
				result.add(entry.getKey());
			}
		}
		return Result.ok(result);
	}
	
}
